<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)


    },
    setOptions({ lasaData, aliData,linzhiData,rikazeData, } = {}) {
// 初始化一个数组用来存放最近七天的日期
      let lastSevenDays = [];

// 获取今天的日期
      let today = new Date();

// 遍历最近七天的日期
      for (let i = 0; i < 7; i++) {
        // 创建一个新日期对象并减去相应的天数
        let pastDate = new Date();
        pastDate.setDate(today.getDate() - i);

        // 格式化日期为 yyyyMMdd 格式
        let year = pastDate.getFullYear();
        let month = (pastDate.getMonth() + 1).toString().padStart(2, '0'); // 月份从0开始，需要+1，并保证是两位数
        let day = pastDate.getDate().toString().padStart(2, '0'); // 保证是两位数

        // 拼接成 yyyyMMdd
        let formattedDate = `${year}${month}${day}`;

        // 将格式化后的日期放入数组
        lastSevenDays.push(formattedDate);
      }

// 对数组从小到大排序
      lastSevenDays.sort();
      this.chart.setOption({
        xAxis: {
          data:lastSevenDays|| ['20241011', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          data: ['拉萨', '林芝','日喀则','阿里',]
        },
        series: [{
          name: '拉萨', itemStyle: {
            normal: {
              color: '#FF005A',
              lineStyle: {
                color: '#FF005A',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          data: lasaData,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        },
        {
          name: '林芝',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#FFB6C1',
              lineStyle: {
                color: '#FFB6C1',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: linzhiData,
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        },
          {
            name: '阿里',
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#00FF00',
                lineStyle: {
                  color: '#00FF00',
                  width: 2
                },
                areaStyle: {
                  color: '#f3f8ff'
                }
              }
            },
            data: aliData,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          },
          {
            name: '日喀则',
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#3888fa',
                lineStyle: {
                  color: '#3888fa',
                  width: 2
                },
                areaStyle: {
                  color: '#f3f8ff'
                }
              }
            },
            data: rikazeData,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          },
          // {
          //   name: '配送中心',
          //   smooth: true,
          //   type: 'line',
          //   itemStyle: {
          //     normal: {
          //       color: '#800080',
          //       lineStyle: {
          //         color: '#800080',
          //         width: 2
          //       },
          //       areaStyle: {
          //         color: '#f3f8ff'
          //       }
          //     }
          //   },
          //   data: lasaData,
          //   animationDuration: 2800,
          //   animationEasing: 'quadraticOut'
          // }
        ]
      })
    }
  }
}
</script>
